﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using NET.Tools.OS;

namespace Puzzler.Plugin.Interface
{
    public abstract class GridPuzzleInputHandler<TModel, TInfo> : PuzzleInputHandler<TModel>
        where TModel : GridPuzzleModel<TInfo>
        where TInfo : GridCellInfo
    {
        protected GridPuzzleInputHandler(TModel model)
            : base(model)
        {
        }

        /// <summary>
        /// Is called if grid cell is clicked
        /// </summary>
        /// <param name="x">x position of grid cell</param>
        /// <param name="y">y position of grid cell</param>
        /// <param name="cellInfo">Cell-Info-Object</param>
        protected virtual void OnGridCellDoubleClick(int x, int y, TInfo cellInfo)
        {
            
        }


        /// <summary>
        /// Is called if key was pressed that is not an arrow key on numpad
        /// </summary>
        /// <param name="cellInfo">Current selected cell info</param>
        /// <param name="key">pressed key</param>
        /// <returns>Selection Running</returns>
        protected virtual void OnKeyPressed(TInfo cellInfo, Key key)
        {
        }

        public override void OnLeftMouseButtonDown(Point point, System.Windows.Input.MouseButtonEventArgs e)
        {
            int x = (int)(point.X / Model.CellWidth);
            int y = (int)(point.Y / Model.CellHeight);
            TInfo cellInfo = Model[x,y];

            if (e.ClickCount == 2)
            {
                if (cellInfo == null)
                    return;

                OnGridCellDoubleClick(x, y, cellInfo);
                return;
            }

            if (cellInfo != null)
            {
                Model.SelectedCell = new Point(x, y);
                return;
            }
        }

        public override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.NumPad2)
            {
                if (!Model.GoSelectionBottom())
                {
                    MessageTools.MessageBeep(MessageType.OK);
                }
            }
            else if (e.Key == Key.NumPad8)
            {
                if (!Model.GoSelectionTop())
                {
                    MessageTools.MessageBeep(MessageType.OK);
                }
            }
            else if (e.Key == Key.NumPad4)
            {
                if (!Model.GoSelectionLeft())
                {
                    MessageTools.MessageBeep(MessageType.OK);
                }
            }
            else if (e.Key == Key.NumPad6)
            {
                if (!Model.GoSelectionRight())
                {
                    MessageTools.MessageBeep(MessageType.OK);
                }
            }
            else
            {
                OnKeyPressed(Model.SelectedCellInfo, e.Key);
            }
        }

        
    }
}
